package com.jky.trial.util;

import javax.activation.MimetypesFileTypeMap;
import java.io.File;

/**
 * 图片工具类
 * ref：http://blog.csdn.net/yjw123456/article/details/70312887
 * Created by suweiming on 2018/2/23.
 */
public class ImgUtils {
    private static MimetypesFileTypeMap mtftp;

    static {
        mtftp = new MimetypesFileTypeMap();
        /* 不添加下面的类型会造成误判 详见：http://stackoverflow.com/questions/4855627/java-mimetypesfiletypemap-always-returning-application-octet-stream-on-android-e*/
        mtftp.addMimeTypes("image png tif jpg jpeg bmp");
    }

    public ImgUtils() {

    }

    /**
     * 判断文件来是否为图片
     *
     * @param file
     * @return
     */
    public static boolean isImage(File file) {
        String mimetype = mtftp.getContentType(file);
        String type = mimetype.split("/")[0];
        return type.equals("image");
    }

    /**
     * 删除文件
     *
     * @param path
     * @return
     */
    public static void delFile(String path) {
        if (path == null) {
            try {
                throw new Exception("path can not be null");
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            File file1 = new File(path);
            if (file1.exists()) {
                file1.delete();
            }
        }

    }
}
